﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using DM_Article.Model;
using DM_Article.Bll;

public partial class admin_admin_articleadd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
            }
            else
            {
                this.txtaddtime.Text = DateTime.Now.ToString();
                getclass();
            }
        }
    }

    private void getclass()
    {
        getdlbigclass();
        getdlsmallclass();
    }

    private void getdlsmallclass()
    {
        int bigclassid = Convert.ToInt32(this.dlbigclass.SelectedValue);
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAccess"].ConnectionString))
        {
            cn.Open();
            this.dlsmallclass.DataSource = smallclassbll.getSmallByBig(cn, bigclassid);
            this.dlsmallclass.DataTextField = "smallclassname";
            this.dlsmallclass.DataValueField = "smallclassid";
            this.dlsmallclass.DataBind();
        }
    }


    private void getdlbigclass()
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAccess"].ConnectionString))
        {
            cn.Open();
            this.dlbigclass.DataSource = bigclassbll.getBigclassTable(cn).DefaultView;
            this.dlbigclass.DataTextField = "bigclassname";
            this.dlbigclass.DataValueField = "bigclassid";
            this.dlbigclass.DataBind();
        }
    }

    private article getarticle()
    {
        article art = new article();
        art.title = this.txttitle.Text.Trim();
        art.keyword = this.txtkeyword.Text.Trim();
        art.content = this.txtcontent.Text.Trim();
        art.bigclassid = Convert.ToInt32(this.dlbigclass.SelectedValue);
        art.smallclassid = Convert.ToInt32(this.dlsmallclass.SelectedValue);
        art.origin = this.txtorigin.Text.Trim();
        art.author = this.txtauthor.Text.Trim();
        art.addtime = this.txtaddtime.Text.Trim();
        return art;
    }

    private bool checkarticle()
    {
        bool ca = true;
        if (this.txttitle.Text == "")
        {
            Response.Write("<script language='javascript'>alert('文章标题不能为空！');</script>");
            this.txttitle.Focus();
            return false;
        }
        if (this.txtcontent.Text == "")
        {
            Response.Write("<script language='javascript'>alert('文章内容不能为空！');</script>");
            this.txtcontent.Focus = true;
            return false;
        }
        return ca;
    }


    protected void btn_addarticle_Click(object sender, EventArgs e)
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAccess"].ConnectionString))
        {
            cn.Open();
            if (checkarticle())
            {
                if (articlebll.add(cn, this.getarticle()) > 0)
                {
                    Response.Write("<script language='javascript'>alert('添加成功！');location.href = 'admin_article.aspx';</script>");
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('添加失败！');location.href = 'admin_articleadd.aspx';</script>");
                }
            }
        }
    }
    protected void btn_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin_article.aspx");
    }
    protected void dlbigclass_SelectedIndexChanged(object sender, EventArgs e)
    {
        getdlsmallclass();
    }
}
